﻿package org.xhs.report.common.utils
{
    import com.greensock.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    public class PageFlip extends EventDispatcher
    {
        protected var isFlipping:Boolean = false;
        protected var shadowLeft:ShadowLeft;
        protected var shadowRight:ShadowRight;
        protected var doCurrent:DisplayObject;
        protected var doPrev:DisplayObject;
        protected var doPrev2:DisplayObject;
        protected var doNext:DisplayObject;
        protected var doNext2:DisplayObject;
        protected var doContainer:DisplayObjectContainer;
        protected var turnLeft:Boolean;
        protected var doAxis:Number;
        protected var tweenLite:TweenLite;
        public static const START_FLIPING:String = "start_flipping";
        public static const FINISH_FLIPING:String = "finish_flipping";

        public function PageFlip()
        {
            this.shadowLeft = new ShadowLeft();
            this.shadowRight = new ShadowRight();
            return;
        }// end function

        public function flip(param1:DisplayObject, param2:DisplayObject, param3:DisplayObject, param4:DisplayObject = null, param5:Boolean = true) : void
        {
            var _loc_8:int = 0;
            if (!param1 || !param2)
            {
                return;
            }
            this.unload();
            this.doPrev = param1;
            this.doPrev2 = param3;
            this.doNext = param2;
            this.doNext2 = param4;
            this.turnLeft = param5;
            this.doNext.scaleX = 0;
            this.doPrev.scaleX = 1;
            this.doAxis = this.doPrev.width + 1;
            this.doNext.x = this.doAxis;
            if (!this.doPrev.parent)
            {
                return;
            }
            this.doContainer = this.doPrev.parent as DisplayObjectContainer;
            this.doContainer.cacheAsBitmap = false;
            var _loc_6:int = 0;
            var _loc_7:* = this.doContainer.numChildren;
            while (_loc_6 < _loc_7)
            {
                
                this.doContainer.getChildAt(_loc_6).visible = false;
                _loc_6++;
            }
            this.doPrev.visible = true;
            this.doNext.visible = true;
            if (this.doPrev2)
            {
                this.doPrev2.scaleX = 1;
                this.doPrev2.visible = true;
                this.doContainer.addChild(this.doPrev2);
            }
            if (this.doNext2)
            {
                this.doNext2.scaleX = 1;
                this.doNext2.visible = true;
                this.doContainer.addChild(this.doNext2);
            }
            this.doContainer.addChild(this.doNext);
            this.doContainer.addChild(this.doPrev);
            _loc_8 = DisplayObjectContainer(this.doPrev).numChildren - 1;
            while (_loc_8 >= 0)
            {
                
                if (DisplayObjectContainer(this.doPrev).getChildAt(_loc_8) is TextField)
                {
                    DisplayObjectContainer(this.doPrev).removeChildAt(_loc_8);
                }
                _loc_8 = _loc_8 - 1;
            }
            _loc_8 = DisplayObjectContainer(this.doNext).numChildren - 1;
            while (_loc_8 >= 0)
            {
                
                if (DisplayObjectContainer(this.doNext).getChildAt(_loc_8) is TextField)
                {
                    DisplayObjectContainer(this.doNext).removeChildAt(_loc_8);
                }
                _loc_8 = _loc_8 - 1;
            }
            this.shadowLeft.scaleX = 1;
            this.shadowLeft.height = this.doPrev.height;
            this.shadowRight.scaleX = 1;
            this.shadowRight.height = this.doPrev.height;
            this.shadowLeft.y = this.doPrev.y;
            this.shadowRight.y = this.doPrev.y;
            this.doContainer.addChild(this.shadowLeft);
            this.doContainer.addChild(this.shadowRight);
            dispatchEvent(new Event(PageFlip.START_FLIPING));
            this.tweenLite = TweenLite.to(this.doPrev, 0.6, {scaleX:-1, onUpdate:this.onPrevUpdate, onComplete:this.flipComplete});
            this.isFlipping = true;
            return;
        }// end function

        protected function onPrevUpdate() : void
        {
            var _loc_1:* = this.doPrev.scaleX * this.doPrev.scaleX;
            this.shadowLeft.alpha = -_loc_1 + 1;
            this.shadowRight.alpha = -_loc_1 + 1;
            this.shadowLeft.scaleX = -_loc_1 + 2;
            this.shadowRight.scaleX = -_loc_1 + 2;
            if (this.doPrev.scaleX > 0)
            {
                this.doCurrent = this.doPrev;
                if (!this.turnLeft)
                {
                    this.doPrev.x = this.doAxis - this.doPrev.width;
                }
            }
            else
            {
                this.doPrev.visible = false;
                if (!this.turnLeft)
                {
                    this.doPrev.x = this.doAxis + this.doPrev.width;
                }
                this.doContainer.addChild(this.doNext);
                this.doNext.scaleX = -this.doPrev.scaleX;
                this.doCurrent = this.doNext;
            }
            if (this.turnLeft)
            {
                this.doNext.x = this.doAxis - this.doNext.width;
            }
            this.shadowLeft.x = this.doCurrent.x;
            this.shadowRight.x = this.doCurrent.x + this.doCurrent.width;
            return;
        }// end function

        protected function flipComplete() : void
        {
            this.isFlipping = false;
            this.unload();
            this.shadowLeft.alpha = 0;
            this.shadowRight.alpha = 0;
            this.doPrev.scaleX = 1;
            this.doPrev.visible = false;
            if (this.doPrev2)
            {
            }
            this.doContainer.addChild(this.doNext);
            this.doNext.scaleX = 1;
            if (!this.turnLeft)
            {
                this.doPrev.x = this.doAxis;
                this.doNext.x = this.doAxis;
            }
            else
            {
                this.doPrev.x = 0;
                this.doNext.x = 0;
            }
            this.tweenLite = null;
            this.doCurrent = null;
            this.doPrev = null;
            this.doNext = null;
            this.doNext2 = null;
            this.isFlipping = false;
            dispatchEvent(new Event(PageFlip.FINISH_FLIPING));
            return;
        }// end function

        protected function flipImmediately() : void
        {
            this.shadowLeft.alpha = 0;
            this.shadowRight.alpha = 0;
            this.doPrev.scaleX = 1;
            this.doPrev.visible = false;
            this.doContainer.addChild(this.doNext);
            this.doNext.scaleX = 1;
            if (!this.turnLeft)
            {
                this.doPrev.x = this.doAxis;
                this.doNext.x = this.doAxis;
            }
            else
            {
                this.doPrev.x = 0;
                this.doNext.x = 0;
            }
            this.tweenLite.kill();
            this.tweenLite = null;
            this.doCurrent = null;
            this.doPrev = null;
            this.doNext = null;
            this.doNext2 = null;
            dispatchEvent(new Event(PageFlip.FINISH_FLIPING));
            return;
        }// end function

        public function unload() : void
        {
            if (this.isFlipping)
            {
                this.flipImmediately();
            }
            if (this.doContainer)
            {
                if (this.doContainer.contains(this.shadowLeft))
                {
                    this.doContainer.removeChild(this.shadowLeft);
                }
                if (this.doContainer.contains(this.shadowRight))
                {
                    this.doContainer.removeChild(this.shadowRight);
                }
            }
            this.isFlipping = false;
            return;
        }// end function

    }
}
